package algotithm.jianzhioffer.test57_II;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/15-10:57
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int target = 9;
        int[][] continuousSequence = solution.findContinuousSequence(target);
        for (int i = 0; i < continuousSequence.length; i++) {
            for (int j = 0; j < continuousSequence[0].length; j++) {
                System.out.println(continuousSequence[i][j]);
            }
        }
    }

    public int[][] findContinuousSequence(int target) {
        List<int[]> list = new ArrayList<>();
        for (int i = 1, j = 2; i < j;) {
            int sum = (i + j) * (j - i + 1) / 2;
            if (sum == target) {
                int[] res = new int[j - i + 1];
                for (int k = i; k <= j; k++) {
                    res[k-i] = k;
                }
                list.add(res);
                i++;
            } else if (sum < target) {
                j++;
            } else {
                i++;
            }
        }
        return list.toArray(new int[list.size()][]);
    }
}